﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Filter
{
    /// <summary>
    /// 过滤、提取
    /// </summary>
    public class JSFilter
    {
        /// <summary>
        /// 提取字符串中的数字（去除非数字字符，得到一个整数结果）
        /// </summary>
        /// <param name="val">源字符串</param>
        /// <returns></returns>
        public static int GetInt(string val)
        {
            try
            {
                return Convert.ToInt32(Regex.Replace(val, @"[^\d]*", ""));
            }
            catch { return 0; }
        }

        /// <summary>
        /// 提取字符串中的数字（小数）
        /// </summary>
        /// <param name="val">源字符串</param>
        /// <returns></returns>
        public static int GetFloat(string val)
        {
            try
            {
                return Convert.ToInt32(Regex.Replace(val, @"[^\d]*", ""));
            }
            catch { return 0; }
        }

        /// <summary>
        /// 生成随机的负数
        /// </summary>
        /// <param name="VcodeNum">生成字母的个数</param>
        /// <returns>string</returns>
        public static int RndNum(int VcodeNum)
        {
            string Vchar = "0,1,2,3,4,5,6,7,8,9";
            string[] VcArray = Vchar.Split(',');
            string VNum = ""; //由于字符串很短，就不用StringBuilder了
            int temp = -1; //记录上次随机数值，尽量避免生产几个一样的随机数

            //采用一个简单的算法以保证生成随机数的不同
            Random rand = new Random();
            for (int i = 1; i < VcodeNum + 1; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(VcArray.Length);
                if (temp != -1 && temp == t)
                {
                    return RndNum(VcodeNum);
                }
                temp = t;
                VNum += VcArray[t];
            }
            //return VNum;
            return 0 - Convert.ToInt32(VNum);
        }

        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="VcodeNum">生成字母的个数</param>
        /// <returns>string</returns>
        public static string RndString(int VcodeNum)
        {
            string Vchar = "0,1,2,3,4,5,6,7,8,9,a,A,b,B,c,C,d,D,e,E,f,F,g,G,h,H,i,I,j,J,k,K,l,L,m,M,n,N,o,O,p,P,q,Q,r,R,s,S,t,T,u,U,v,V,w,W,x,X,y,Y,z,Z";
            string[] VcArray = Vchar.Split(',');
            string VNum = ""; //由于字符串很短，就不用StringBuilder了
            int temp = -1; //记录上次随机数值，尽量避免生产几个一样的随机数

            //采用一个简单的算法以保证生成随机数的不同
            Random rand = new Random();
            for (int i = 1; i < VcodeNum + 1; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(VcArray.Length);
                if (temp != -1 && temp == t)
                {
                    return RndString(VcodeNum);
                }
                temp = t;
                VNum += VcArray[t];
            }
            return VNum;
        }

        /// <summary>
        /// 获取文件后缀名
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private static string GetEx(string file)
        {
            string result = "";
            int i = file.LastIndexOf('.');
            if (i > 0)
            {
                result = file.Substring(i + 1);
            }
            return result;
        }
        /// <summary>
        /// 获取文件后缀名（小写）
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string GetFileEx2Lower(string file)
        {
            return GetEx(file).ToLower();
        }
        /// <summary>
        /// 获取文件后缀名（大写）
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string GetFileEx2Upper(string file)
        {
            return GetEx(file).ToUpper();
        }
    }
}
